<template>
    <div class="icon-select">
        <el-input v-model="iconName" clearable placeholder="请输入图标名称" @clear="filterIcons" @input="filterIcons">
            <template #suffix><i class="el-icon-search el-input__icon" /></template>
        </el-input>
        <div class="icon-select__list">
            <div v-for="(item, index) in iconList" :key="index" @click="selectedIcon(item)">
                <svg-icon color="#999" :icon-class="item" style="height: 30px; width: 30px; margin:0 auto" />
                <span>{{ item }}</span>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue';
import SvgIcon from '@/components/Svgicon/index.vue';

const icons = [];
const modules = import.meta.glob('../../assets/catSvg/*.svg');
for (const path in modules) {
    const p = path.split('assets/catSvg/')[1].split('.svg')[0];
    icons.push(p);
}
const iconList = ref(icons);

const iconName = ref('');

const emit = defineEmits(['selected']);

function filterIcons() {
    iconList.value = icons;
    if (iconName.value) {
        iconList.value = icons.filter(item => item.indexOf(iconName.value) !== -1);
    }
}

function selectedIcon(name) {
    emit('selected', name);
    document.body.click();
}

function reset() {
    iconName.value = '';
    iconList.value = icons;
}

defineExpose({
    reset
});
</script>

<style lang="scss" scoped>
.icon-select {
    width: 100%;
    padding: 10px;

    &__list {
        display: flex;
        margin-top: 10px;
        flex-wrap: wrap;

        div {
            border: 1px solid #eee;
            text-align: center;
            width: 70px;
            height: 70px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-content: center;
            margin-right: -1px;
            margin-bottom: -1px;
            cursor: pointer;
        }

        span {
            display: inline-block;
            vertical-align: -0.15em;
            fill: currentColor;
            overflow: hidden;
        }
    }
}
</style>
